package dj.common.lang.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author qiant
 * @date 2022/2/21
 */
@Component
@Slf4j
public class PhoneMessageUtil {

    //服务地址:非必须
    @Value(value = "${message.phone.url:#{null}}")
    public String pushServerUrl;

    public String sendSms(String phone, String message,String accessnumber){
        if(StringUtils.isEmpty(pushServerUrl)){
            return "地址信息未获取";
        }
        JSONObject jo = new JSONObject();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        jo.put("clientId","mportal");
        jo.put("msgContent",message);
        jo.put("timestamp",timestamp);
        String sign = "mportal" + timestamp + "MOBCB123";
        sign = Security.encryptToMD5(sign).toLowerCase();
        jo.put("sign",sign);
        jo.put("receivers",phone);
        String body = jo.toString();
        String[] params;
        params = new String[]{"accessnumber="+accessnumber};
        log.info("PhoneMessageUtil.sendSms.jo:{},params:{}",body,params.toString());
        String result = "";
        for(int x=0;x<3;x++){
            result = HttpUtils.httpPostJson(pushServerUrl + "sms/send",
                    params, "internal http client", body);
            if (StringUtils.isNotEmpty(result)) {
                break;
            }
        }
        return result;
    }
}
